﻿using Microsoft.SharePoint.Administration;

namespace SPSDeploy.Extensions.Extensions
{
    /// <summary>
    /// Service application extensions
    /// </summary>
    internal static class ServiceApplicationExtension
    {

        /// <summary>
        /// Return service application instance
        /// </summary>
        /// <param name="name"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        internal static SPServiceApplication GetServiceApplication(string name, string type)
        {
            SPFarm farm = SPFarm.Local;
            foreach (SPService service in farm.Services)
                foreach (SPServiceApplication application in service.Applications)
                    if (application.DisplayName == name && application.TypeName == type)
                        return application;
            return null;
        }

        /// <summary>
        /// Check if service application exists
        /// </summary>
        /// <param name="services"></param>
        /// <param name="name"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        internal static bool ServiceApplicationExists(SPServiceCollection services, string name, string type)
        {
            foreach (SPService service in services)
                foreach (SPServiceApplication application in service.Applications)
                    if (application.DisplayName == name && application.TypeName == type)
                        return true;
            return false;
        }

    }
}